package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.CompanyDao;
import com.cn.wanxi.dao.impl.CompanyImpl;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.service.ICompanyService;
import redis.clients.jedis.Jedis;

public class CompanyServiceImpl implements ICompanyService {
    /**
     * 判断是否需要用到缓存？
     * 1.如果有大量用户访问时！为了避免多用户同时访问造成数据库压力过大，
     * 2.第一次因为没有数据，则需要从数据库里面取得数据，
     * 3.如果已经取得数据，则需要将取得的数据保持到缓存
     * 4.为了在有都二哥以上的用户访问时，直接通过缓存取得数据
     *
     * @return
     */
    @Override
    public CompanyModel getCompanyModel() {
//        引用传值
//        值传递
//        final CompanyModel[] companyModel = {new CompanyModel()};
//        for (int i = 0; i < 500; i++) {
//            new Thread(
//                    new Runnable() {
//                        @Override
//                        public void run() {
//                            System.out.println(Thread.currentThread().getName());
//                            companyModel[0] = getData();
//                        }
//                    }
//                    , "线程" + i).start();
//        }
//        return companyModel[0];
        CompanyDao companyDao = new CompanyImpl();
        return companyDao.getCompanyModel();
    }

    private synchronized CompanyModel getData() {
        boolean isHave = false;//判断缓存是否有数据
        CompanyModel companyModel = null;
        Jedis jedis = new Jedis();
        isHave = jedis.exists("logo");
        if (isHave) {
            companyModel = new CompanyModel();
            String logo = jedis.get("logo");
            companyModel.setLogo(logo);
            companyModel.setPhone(jedis.get("phone"));
            companyModel.setNet(jedis.get("net"));
            companyModel.setEmail(jedis.get("email"));
            companyModel.setLinePhone(jedis.get("linePhone"));
            companyModel.setAd(jedis.get("ad"));
            companyModel.setAddress(jedis.get("address"));
            companyModel.setVersion(jedis.get("version"));
            System.out.println("redis");
        } else {
//            数据库的数据
//            将数据库数据存入缓存
            CompanyDao companyDao = new CompanyImpl();
            companyModel = companyDao.getCompanyModel();
            jedis.set("logo", companyModel.getLogo());
            jedis.set("phone", companyModel.getPhone());
            jedis.set("net", companyModel.getNet());
            jedis.set("email", companyModel.getEmail());
            jedis.set("linePhone", companyModel.getLinePhone());
            jedis.set("version", companyModel.getVersion());
            jedis.set("address", companyModel.getAddress());
            jedis.set("ad", companyModel.getAd());
//            jedis.mset("logo", companyModel.getLogo(), "phone", companyModel.getPhone());
            System.out.println("mysql");
        }
        return companyModel;//缓存数据
    }

    /**
     * 只要对数据进行更新，都应该去清空key值，以保证每次取得数据为最新
     * 当用户，管理员修改公司信息时，应该清空redis，
     */
    public int update(CompanyModel companyModel) {
        CompanyDao companyDao = new CompanyImpl();
        int result = companyDao.update(companyModel);
        if (result == 1) {
            Jedis jedis = new Jedis();
            jedis.del("logo");
        }
        return result;
    }
}
